package com.moon.game.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.moon.game.resource.dao.AnimalChildDAO;
import com.moon.game.resource.dao.AnimalChildDAOImpl;
import com.moon.game.resource.dao.AnimalDAO;
import com.moon.game.resource.dao.AnimalDAOImpl;
import com.moon.game.resource.dao.FeteDAO;
import com.moon.game.resource.dao.FeteDAOImpl;
import com.moon.game.resource.dao.GiftDAO;
import com.moon.game.resource.dao.GiftDAOImpl;
import com.moon.game.resource.dao.ItemDAO;
import com.moon.game.resource.dao.ItemDAOImpl;
import com.moon.game.resource.dao.LevelExpDAO;
import com.moon.game.resource.dao.LevelExpDAOImpl;
import com.moon.game.resource.dao.MainLandDAO;
import com.moon.game.resource.dao.MainLandDAOImpl;
import com.moon.game.resource.dao.MainLandPortsDAO;
import com.moon.game.resource.dao.MainLandPortsDAOImpl;
import com.moon.game.resource.dao.MissionDAO;
import com.moon.game.resource.dao.MissionDAOImpl;
import com.moon.game.resource.dao.MonsterDAO;
import com.moon.game.resource.dao.MonsterDAOImpl;
import com.moon.game.resource.dao.NoticeDAO;
import com.moon.game.resource.dao.NoticeDAOImpl;
import com.moon.game.resource.dao.NpcsDAO;
import com.moon.game.resource.dao.NpcsDAOImpl;
import com.moon.game.resource.model.Animal;
import com.moon.game.resource.model.AnimalChild;
import com.moon.game.resource.model.Fete;
import com.moon.game.resource.model.Gift;
import com.moon.game.resource.model.Item;
import com.moon.game.resource.model.LevelExp;
import com.moon.game.resource.model.MainLand;
import com.moon.game.resource.model.MainLandPorts;
import com.moon.game.resource.model.Mission;
import com.moon.game.resource.model.Monster;
import com.moon.game.resource.model.Notice;
import com.moon.game.resource.model.Npcs;
import com.moon.game.util.SqlMapConfigData;

/**
 * @description 
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-29 下午3:17:56
 */
public class DBManager {
	
	private static final Logger log = Logger.getLogger(DBManager.class);
	/**
	 * DAOS
	 **/
	private static Map<String, Object> daoDataMap = new HashMap<String, Object>();
	/**
	 * 对应xx_res库的dao
	 * 
	 * @param clazz
	 * @return
	 */
	public static Object getDataDao(Class clazz) {
		Object o = daoDataMap.get(clazz.getName());
		if (o != null)
			return o;
		try {
			o = clazz.getConstructor(SqlMapClient.class).newInstance(SqlMapConfigData.getSqlMapInstance());
			daoDataMap.put(clazz.getName(), o);
			return o;
		} catch (Exception e) {
			log.error("exception", e);
		}
		return null;
	}
	
	
	/****************************************************
	 * 加载各类资源
	 */
	
	
	/**
	 * 查询怪物资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<Monster> queryAllMonsters() {
		List<Monster> list = null;
		try {
			MonsterDAO dao = (MonsterDAOImpl) getDataDao(MonsterDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<Monster>(0) : list);
	}
	
	/**
	 * 查询大陆资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<MainLand> queryAllMainLand() {
		List<MainLand> list = null;
		try {
			MainLandDAO dao = (MainLandDAOImpl) getDataDao(MainLandDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<MainLand>(0) : list);
	}
	
	/**
	 * 查询大陆通道口资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<MainLandPorts> queryAllMainLandPorts() {
		List<MainLandPorts> list = null;
		try {
			MainLandPortsDAO dao = (MainLandPortsDAOImpl) getDataDao(MainLandPortsDAOImpl.class);
			list = dao.selectByExample(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<MainLandPorts>(0) : list);
	}
	
	/**
	 * 查询NPC资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<Npcs> queryAllNPC() {
		List<Npcs> list = null;
		try {
			NpcsDAO dao = (NpcsDAOImpl) getDataDao(NpcsDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<Npcs>(0) : list);
	}
	
	/**
	 * 查询NPC资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<Item> queryAllItem() {
		List<Item> list = null;
		try {
			ItemDAO dao = (ItemDAOImpl) getDataDao(ItemDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<Item>(0) : list);
	}
	
	
	/**
	 * 查询等级升级对应的经验资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<LevelExp> queryAllLevelExp() {
		List<LevelExp> list = null;
		try {
			LevelExpDAO dao = (LevelExpDAOImpl) getDataDao(LevelExpDAOImpl.class);
			list = dao.selectByExample(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<LevelExp>(0) : list);
	}
	
	
	/**
	 * 查询可以被召唤的珍兽
	 **/
	@SuppressWarnings("unchecked")
	public static List<Animal> queryAllAnimals() {
		List<Animal> list = null;
		try {
			AnimalDAO dao = (AnimalDAOImpl) getDataDao(AnimalDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<Animal>(0) : list);
	}
	
	/**
	 * 查询珍兽对应的孵化资源
	 **/
	@SuppressWarnings("unchecked")
	public static List<AnimalChild> queryAllAnimalsChild() {
		List<AnimalChild> list = null;
		try {
			AnimalChildDAO dao = (AnimalChildDAOImpl) getDataDao(AnimalChildDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("load item has exception.", e);
		}
		return (list == null ? new ArrayList<AnimalChild>(0) : list);
	}
	
	/**
	 * 查询公告
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Notice> queryAllNotices() {
		List<Notice> list = null;
		try {
			NoticeDAO dao = (NoticeDAO) DBManager.getDataDao(NoticeDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("exception", e);
		}
		return (list == null ? new ArrayList<Notice>(0) : list);
	}
	
	/**
	 * 查询所有任务
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Mission> queryAllMissions() {
		List<Mission> list = null;
		try {
			MissionDAO dao = (MissionDAO) DBManager.getDataDao(MissionDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("exception", e);
		}
		return (list == null ? new ArrayList<Mission>(0) : list);
	}
	
	
	/**
	 * 查询祭祀配置
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Fete> queryFetes() {
		List<Fete> list = null;
		try {
			FeteDAO dao = (FeteDAO) DBManager.getDataDao(FeteDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("exception", e);
		}
		return (list == null ? new ArrayList<Fete>(0) : list);
	}
	
	/**
	 * 查询祭祀配置
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Gift> queryGiftConfig() {
		List<Gift> list = null;
		try {
			GiftDAO dao = (GiftDAO) DBManager.getDataDao(GiftDAOImpl.class);
			list = dao.selectByExampleWithBLOBs(null);
		} catch (SQLException e) {
			e.printStackTrace();
			log.error("exception", e);
		}
		return (list == null ? new ArrayList<Gift>(0) : list);
	}
}
